package com.snda.storage.service.sdk;

import static com.google.common.collect.Iterables.addAll;

import java.util.List;

import javax.annotation.concurrent.Immutable;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

/**
 * 
 * @author wangzijian@snda.com
 * 
 */
@Immutable
public final class ObjectListing extends ValueObject {
	private final String bucketName;
	private final String nextMarker;
	private final boolean truncated;
	private final String prefix;
	private final String marker;
	private final int maxKeys;
	private final String delimiter;
	private final List<String> commonPrefixes;
	private final List<ObjectSummary> objectSummaries;

	private ObjectListing(Builder builder) {
		this.bucketName = builder.bucketName;
		this.nextMarker = builder.nextMarker;
		this.truncated = builder.truncated;
		this.prefix = builder.prefix;
		this.marker = builder.marker;
		this.maxKeys = builder.maxKeys;
		this.delimiter = builder.delimiter;
		this.commonPrefixes = ImmutableList.copyOf(builder.commonPrefixes);
		this.objectSummaries = ImmutableList.copyOf(builder.objectSummaries);
	}

	public String getBucketName() {
		return bucketName;
	}

	public String getNextMarker() {
		return nextMarker;
	}

	public boolean isTruncated() {
		return truncated;
	}

	public String getPrefix() {
		return prefix;
	}

	public String getMarker() {
		return marker;
	}

	public int getMaxKeys() {
		return maxKeys;
	}

	public String getDelimiter() {
		return delimiter;
	}

	public List<ObjectSummary> getObjectSummaries() {
		return objectSummaries;
	}

	public List<String> getCommonPrefixes() {
		return commonPrefixes;
	}

	public static final class Builder {
		private String bucketName;
		private String nextMarker;
		private Boolean truncated;
		private String prefix;
		private String marker;
		private Integer maxKeys;
		private String delimiter;
		private List<ObjectSummary> objectSummaries = Lists.newArrayList();;
		private List<String> commonPrefixes = Lists.newArrayList();

		public ObjectListing build() {
			return new ObjectListing(this);
		}

		public Builder bucketName(String bucketName) {
			this.bucketName = bucketName;
			return this;
		}

		public Builder nextMarker(String nextMarker) {
			this.nextMarker = nextMarker;
			return this;
		}

		public Builder truncated(Boolean truncated) {
			this.truncated = truncated;
			return this;
		}

		public Builder prefix(String prefix) {
			this.prefix = prefix;
			return this;
		}

		public Builder marker(String marker) {
			this.marker = marker;
			return this;
		}

		public Builder maxKeys(Integer maxKeys) {
			this.maxKeys = maxKeys;
			return this;
		}

		public Builder delimiter(String delimiter) {
			this.delimiter = delimiter;
			return this;
		}

		public Builder objectSummary(ObjectSummary objectSummary) {
			this.objectSummaries.add(objectSummary);
			return this;
		}

		public Builder objectSummaries(Iterable<ObjectSummary> objectSummaries) {
			addAll(this.objectSummaries, objectSummaries);
			return this;
		}

		public Builder commonPrefix(String commonPrefix) {
			this.commonPrefixes.add(commonPrefix);
			return this;
		}

		public Builder commonPrefixes(List<String> commonPrefixes) {
			addAll(this.commonPrefixes, commonPrefixes);
			return this;
		}
	}

}
